package facade;

import general.NoPrinterInstalledFoundException;
import general.PrinterDialog;
import grammar.GrammarRepository;
import grammar.perspective.CanNotLoadGrammarOnScreenException;
import grammar.perspective.GrammarHasNotSyntaxAssociatedException;
import grammar.perspective.GrammarNotExistException;
import grammar.perspective.GrammarRegister;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.print.PrintException;

import DTO.Aspect;
import DTO.Grammar;
import DTO.ModelGenerated;
import aspect.AspectRepository;
import business.AspectFile;
import development.perpective.AspectRegister;
import development.perpective.ExportArchive;

public class Facade {
	private static Facade instance;
	
	private PrinterDialog printers;
	private ExportArchive exportArchive;

	private GrammarRegister grammarRegister;
	private AspectRegister aspectRegister;

	/* -------------------- Builder -------------------- */
	private Facade(){
		printers = new PrinterDialog();
		exportArchive = new ExportArchive();

		aspectRegister = new AspectRegister(new AspectRepository());
		grammarRegister = new GrammarRegister(new GrammarRepository());
	}
	
	/* -------------------- Instance methods -------------------- */
	public static Facade getInstance(){
		if(instance == null){
			instance = new Facade();
		}
		
		return instance;
	}
	
	/* -------------------- Printer methods -------------------- */
	public void printText(String textToBePrint) throws NoPrinterInstalledFoundException, PrintException{
		printers.printText(textToBePrint);
	}
	
	/* -------------------- I/O methods -------------------- */
	public void exportAspectArchive(String codeToExport){
		exportArchive.exportAspectArchive(codeToExport);
	}

	/* -------------------- Grammar methods -------------------- */
	public Grammar getLastGrammar() throws SQLException, CanNotLoadGrammarOnScreenException{
		return grammarRegister.getGrammar();
	}

	public void setNewGrammar(Grammar grammar) throws SQLException, GrammarNotExistException, GrammarHasNotSyntaxAssociatedException{
		grammarRegister.setGrammar(grammar);
	}

	/* -------------------- Aspect and Properties methods -------------------- */
	public AspectFile addAspect(Aspect aspect, String[] propertiesNames) throws SQLException{
		return aspectRegister.addAspect(aspect, propertiesNames, exportArchive);
	}

	public String[] getPropertiesNames() throws SQLException{
		return aspectRegister.getPropertiesNames();
	}
	
	public ArrayList<Aspect> getPropertiesAndAspects() throws SQLException{
		return aspectRegister.getPropertiesAndAspects();
	}
	
	public void joinCode(ModelGenerated modelToExport) throws Exception{
		aspectRegister.joinCode(modelToExport);
	}
}
